/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the
 * GPL version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin.prefs;

import org.digitalsoul.switchplugin.prefs.util.HelpTexts;
import org.digitalsoul.switchplugin.prefs.util.ItemListConfigurator;
import org.digitalsoul.switchplugin.prefs.util.UITools;
import org.digitalsoul.switchplugin.prefs.wizards.ActionWizard;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

public class ActionListUI extends CriteriaListUI {

    private Button upButton;
    private Button downButton;
    
    public ActionListUI(ItemListConfigurator configurator) {
        super(configurator);
    }

    @Override
    protected String getHeader() {
        return "Actions";
    }

    @Override
    protected String getHelpText() {
        return HelpTexts.getActionsHelp();
    }

    @Override
    protected String[] getListItems() {
        return configurator.getConfiguredActionDescriptions();
    }

    @Override
    protected IWizard getWizard() {
        return new ActionWizard(configurator, this);
    }

    @Override
    protected void removeItem(int selectionIndex) {
        configurator.removeAction(selectionIndex);
    }
    
    @Override
    protected void buildListView(Composite mainPanel) {
        super.buildListView(mainPanel);
        Composite upDownPanel = UITools.createGridPanel(mainPanel, 2);
        GridData data = new GridData();
        data.horizontalAlignment = GridData.END;
        upDownPanel.setLayoutData(data);
        upButton = UITools.addButton(upDownPanel, "up");
        upButton.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event arg0) {
                int index = list.getSelectionIndex();
                configurator.moveActionUp(index);
                updateListItems();
                list.select(index-1);
                updateButtons();
            }
        });
        
        downButton = UITools.addButton(upDownPanel, "down");
        downButton.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event arg0) {
                int index = list.getSelectionIndex();
                configurator.moveActionDown(index);
                updateListItems();
                list.select(index+1);
                updateButtons();
            }
        });
    }
    @Override
    public void updateButtons() {
        super.updateButtons();
        upButton.setEnabled(list.getSelectionIndex() > 0);
        downButton.setEnabled(list.getSelectionIndex() >= 0 && list.getSelectionIndex() < list.getItemCount()-1);
    }
}
